package com.jjlin.leetcode;

import java.util.HashMap;
import java.util.Map;

public class Solution1 {
    public int[] twoSum(int[] nums, int target) {
        if(nums == null || nums.length < 2)
            return new int[0];
        Map<Integer, Integer> map = new HashMap<>();//key存储元素，value存储位置
        int len = nums.length;
        int[] res = new int[2];
        for(int i = 0; i < len; i++){
            if(map.containsKey(target - nums[i])){
                res[0] = Math.min(i, map.get(target - nums[i]));
                res[1] = Math.max(i, map.get(target - nums[i]));
                break;
            }
            map.put(nums[i], i);
        }
        return res;
    }
}

